import Vue from 'vue'
import Router from 'vue-router'
import routes from './router'
// import store from '@/store/index'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getUserId, getToken } from '@/libs/util'
import config from '@/config/index'
Vue.use(Router)

const router = new Router({
  // mode: 'history', // default: hash ,history 这样设置后，就可以去掉url中丑丑的“#”啦
  routes // (缩写) 相当于 routes: routes
})
/*
const LOGIN_PAGE_NAME = 'login'

router.beforeEach((to, from, next) => {
  const token = getToken()
  const userId = getUserId()
  console.log('token', token)
  console.log('userId', userId)
  if (!userId && !token && to.name !== LOGIN_PAGE_NAME) {
    NProgress.start()
    // 未登录且要跳转的页面不是登录页
    next({
      name: config.homeName // 跳转到登录页
    })
  } else if (!userId && !token && to.name === LOGIN_PAGE_NAME) {
    NProgress.start()
    // 未登陆且要跳转的页面是登录页
    next({
      name: config.homeName // 跳转到登录页
    }) // 跳转
  } else if (!userId && token && to.name === LOGIN_PAGE_NAME) {
    NProgress.start()
    // 已登录且要跳转的页面是登录页
    next({
      name: config.homeName // 跳转到homeName页
    })
  } else {
    NProgress.done()
    next()
  }
})
*/
router.afterEach(to => {
  NProgress.start()
  setTimeout(() => {
    NProgress.done()
  }, 500)
})

export default router
